﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using Innova.Microfinanzas.BusinessEntities;
using MyCommerce.Presentation.UsersAndSecurity.Presenters;

namespace MyCommerce.Windows.UsersAndSecurity.Views
{
    public partial class CommandInRoleView : BaseForm, ICommandInRoleView
    {
        private CommandInRolePresenter pPresenter;

        public CommandInRoleView()
        {
            InitializeComponent();
            pPresenter = new CommandInRolePresenter(this);
        }

        #region ICommandInRoleView Members

        public List<Command> ColCommandsLoaded
        {
            set 
            {
                listCommands.SubLevelCheckBox = true;
                listCommands.ShowSubLevelCheckBox = false;
                listCommands.SetNodes<Command>(value, "Title", "IdCommand", "ColChildCommands");
            }
        }

        public long IdRoleLoad
        {
            get;
            set;
        }

        public IList<long> SelectedCommands
        {
            get
            {
                return listCommands.GetSelectedEntitiesIdList();
            }
            set
            {
                listCommands.SetSelectedEntitiesIdList(value);
                listCommands.CheckFatherNode = true;
            }
        }

        public void CloseAndSetResult(bool result)
        {
            if (result)
                Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de usuarios.");
            Result = result;
            Close();
        }

        public event EventHandler OnLoadView;

        public event EventHandler OnSave;

        public event EventHandler OnCancel;

        #endregion

        private void CommandInRoleView_Load(object sender, EventArgs e)
        {
            if (Parameter != null)
            {
                IdRoleLoad = Convert.ToInt64(Parameter);
                if (OnLoadView != null)
                    OnLoadView(this, new EventArgs());
            }
            else
            {
                Context.ShowInformationMessageBox("No existe el rol para poder cargar sus privilegios.", "Mensaje");
                Close();
            }
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCancel != null)
                OnCancel(this, new EventArgs());
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (OnSave != null)
                    OnSave(this, new EventArgs());
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }
    }
}
